import os
import copy
import importlib
from scopt.util.misc import dir_name
from scopt.util.message_types import STRING

I18N = {'security': os.path.join(dir_name(__file__), 'i18n')}


def registry(config):
    modul_name = config.get('SECURITY', 'module')
    registry_modul = importlib.import_module(modul_name)
    return registry_modul.Registry(config)


class TokenEnhancer(object):
    def __init__(self):
        self.__registry = {}

    def __call__(self, version, token, resd=None):
        def enhance(f):
            f.TYPE = token['type']
            f.VERSION = version
            f.TOKEN = _token(token)
            f.RESPONSE = _response(resd)
            self.__registry.update({f.TYPE: f})
            return f
        return enhance

    def registry(self):
        return self.__registry


def _token(token):
    def f():
        return copy.deepcopy(token)
    return f


def _response(resd):
    def f():
        return {'resh': response_header_template(),
                'resd': copy.deepcopy(resd)}
    return f


def response_header_template():
    return {'message_id': STRING(required=True),
            'status': STRING(required=True)}
